package com.zg.scf.util;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zg.common.core.User;
import com.zg.common.core.dao.scf.ScfUserCompanyDao;
import com.zg.common.core.dao.scf.entity.ScfUserCompany;
import com.zg.common.web.Req;
import com.zg.scf.enm.UserTypeEnum;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.*;

@Component
@RequiredArgsConstructor
public class DataScopeUtil {

    private final ScfUserCompanyDao scfUserCompanyDao;

    public Long getOrgDataScope() {
        User currUser = Req.getCurrUser();
        if (UserTypeEnum.SYSTEM.getCode().equals(currUser.getType())) {
            return null;
        } else {
            List<ScfUserCompany> list = scfUserCompanyDao.list(
                    new LambdaQueryWrapper<ScfUserCompany>()
                            .eq(ScfUserCompany::getUserId, currUser.getId())
            );
            if (list.size() > 0) {
                return list.get(0).getCompanyId();
            } else {
                return -1L;
            }
        }
    }
}
